using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;

namespace Zephyris_Space_Mercs
{
    class SettingsFileHandler
    {
        private string sFile = "";
        private IsolatedStorageFile issfile;
        DataContractSerializer saveSerializer = new DataContractSerializer(typeof(SettingsData));

        private void InitSaveFileHandler()
        {
            this.issfile = IsolatedStorageFile.GetUserStoreForApplication();
            this.sFile = "SpaceMercs_Settings.xml";
        }

        public void SaveData(SettingsData data)
        {
            InitSaveFileHandler();

            using (var targetFile = issfile.CreateFile(sFile))
            {
                saveSerializer.WriteObject(targetFile, data);
            }
        }

        public SettingsData ReadData()
        {
            InitSaveFileHandler();

            using (var sourceStream = new IsolatedStorageFileStream(this.sFile, FileMode.OpenOrCreate, issfile))
            {
                SettingsData readSettings = new SettingsData();
                try
                {
                    readSettings = (SettingsData)saveSerializer.ReadObject(sourceStream);
                }
                catch (SerializationException)
                {
                }
                return readSettings;
            }

        }
    }
}
